package com.swt.gap.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.swt.gap.common.PageResult;
import com.swt.gap.dao.EntityDictDao;
import com.swt.gap.dao.OntologyNodeDao;
import com.swt.gap.model.dto.AddEntityDictDTO;
import com.swt.gap.model.entity.EntityDict;
import com.swt.gap.model.entity.OntologyNode;
import com.swt.gap.model.vo.EntityDictVO;
import com.swt.gap.service.EntityDictService;
import com.swt.gap.util.UserLoginUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author sx
 */
@Service
public class EntityDictServiceImpl implements EntityDictService {

    private final EntityDictDao entityDictDao;
    private final OntologyNodeDao ontologyNodeDao;

    public EntityDictServiceImpl(EntityDictDao entityDictDao, OntologyNodeDao ontologyNodeDao) {
        this.entityDictDao = entityDictDao;
        this.ontologyNodeDao = ontologyNodeDao;
    }

    @Override
    @Async
    public void addDictEntity(AddEntityDictDTO record) {
        String[] texts = record.getText().replace("\\n", ",").split(",");
        for (String text : texts) {
            EntityDict entityDict = EntityDict.builder()
                    .createdTime(new Date())
                    .createdBy(UserLoginUtils.getUser().getUserName())
                    .text(text.trim())
                    .firstOntologyNodeId(record.getFirstOntologyNodeId())
                    .secondOntologyNodeId(record.getSecondOntologyNodeId())
                    .build();
            entityDictDao.insertSelective(entityDict);
        }
    }

    @Override
    public PageResult<EntityDictVO> listDictEntities(Integer pageNum, Integer pageSize) {
        PageResult<EntityDictVO> retValue = new PageResult();
        List<EntityDictVO> listData = new ArrayList<>();
        if (null == pageNum || null == pageSize) {
            pageNum = 1;
            pageSize = 10;
        }
        PageHelper.startPage(pageNum, pageSize);
        Page<EntityDict> res = entityDictDao.selectAll();
        res.getResult().forEach(dict -> {
            EntityDictVO retEntityDict = new EntityDictVO();
            BeanUtils.copyProperties(dict, retEntityDict);
            String firstLabels = ontologyNodeDao.selectByPrimaryKey(dict.getFirstOntologyNodeId()).getLabels();
            retEntityDict.setFirstLevelOntologyNode(firstLabels.substring(firstLabels.lastIndexOf("," ) + 1));
            OntologyNode secondOntologyNode = ontologyNodeDao.selectByPrimaryKey(dict.getSecondOntologyNodeId());
            if (null != secondOntologyNode) {
                String secondLabels = secondOntologyNode.getLabels();
                retEntityDict.setSecondLevelOntologyNode(secondLabels.substring(secondLabels.lastIndexOf(",") + 1));
            }
            listData.add(retEntityDict);
        });
        return new PageResult<>(listData, (int)res.getTotal(), res.getPageSize(), res.getPageNum());
    }

    @Override
    public Integer deleteDict(Integer dictId) {
        return entityDictDao.deleteByPrimaryKey(dictId);
    }

    @Override
    public Integer batchDeleteDict(List<Integer> dictIds) {
        int retValue = 0;
        for(Integer dictId: dictIds) {
            retValue += entityDictDao.deleteByPrimaryKey(dictId);
        }
        return retValue;
    }

    @Override
    public Integer updateDict(EntityDict entityDict) {
        return entityDictDao.updateByPrimaryKeySelective(entityDict);
    }
}
